# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gimp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_basever=2.10.18
_prever=
pkgver=${_basever}${_prever//-/}
pkgrel=6
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
url="https://www.gimp.org/"
license=('GPL' 'LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-gegl"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib-networking"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python2-pygtk"
         "${MINGW_PACKAGE_PREFIX}-python2-gobject2"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pygobject2-devel"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "intltool")
options=('strip' 'makeflags')
install=gimp-${CARCH}.install
source=(https://download.gimp.org/pub/gimp/v${_basever%.*}/${_realname}-${_basever}${_prever}.tar.bz2
        0001-find-directx-fix.patch
        0002-python-m4.patch
        0003-mingw-add-ModuleFileName-dir-to-PATH.patch
        0004-replace-pygtk-codegen.patch
        0005-fix-link-python-plugins.patch)
sha256sums=('65bfe111e8eebffd3dde3016ccb507f9948d2663d9497cb438d9bb609e11d716'
            'f7f9badc547f1a0317b95e012530db8df63b7c7038c9d9ff8d0b510c86f43439'
            'ae9b6e8fa40fd471cb58f17454219abf531565b5fbaf616e5fc81b3869bbc4b5'
            '682656d1f7ec70bfd3dd10eba3f8db8abb75c80e44933f8c09c730cea5aeb549'
            'e7cc5603c51517add367d8062eb3a28d5b6c51eded6cb51f749c4570f27b91a7'
            '01651bb582b2120aae67752f6a28c0750ea690d46259d855e28f8b18da567bf5')

prepare() {
  cd "${srcdir}/${_realname}-${_basever}${_prever}"
  patch -p1 -i ${srcdir}/0001-find-directx-fix.patch
  patch -p1 -i ${srcdir}/0002-python-m4.patch
  patch -p1 -i ${srcdir}/0003-mingw-add-ModuleFileName-dir-to-PATH.patch
  patch -p1 -i ${srcdir}/0004-replace-pygtk-codegen.patch
  patch -p1 -i ${srcdir}/0005-fix-link-python-plugins.patch

  rm -fr autom4te.cache
  libtoolize -c
  aclocal -I m4macros
  automake -a -c
  autoheader
  autoconf

  #autoreconf -fi -I m4macros
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  cp -rf ${_realname}-${_basever}${_prever} build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  PYTHON=${MINGW_PREFIX}/bin/python2 \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --enable-mp \
    --enable-gimp-console \
    --enable-python \
    --without-aa \
    --with-pdbgen \
    --with-directx-sdk=${MINGW_PREFIX}/${MINGW_CHOST} \
    --without-x \
    --without-webkit

  # V=1 because of a race condition (a generated 'bin2c' type
  # file ending up truncated once). This might avoid it.
  make V=1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  ln -s gimptool-2.0 "${pkgdir}${MINGW_PREFIX}/bin/gimptool.exe"
  sed -e "s|${MINGW_PREFIX}/bin/python2|python2w.exe|g" -i ${pkgdir}${MINGW_PREFIX}/lib/gimp/2.0/interpreters/pygimp.interp
  rm -f "${pkgdir}${MINGW_PREFIX}/lib/*.def"
  rm -f "${pkgdir}${MINGW_PREFIX}/lib/gimp/2.0/modules/*.dll.a"

  cp ${pkgdir}${MINGW_PREFIX}/bin/gimp-${pkgver%.*}.exe ${pkgdir}${MINGW_PREFIX}/bin/gimp.exe
}
